# -*- coding: utf-8 -*-
#
# File: jornal.py
#
# Copyright (c) 2007 by LM
# Generator: ArchGenXML Version 1.5.1 svn/devel
#            http://plone.org/products/archgenxml
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """LM <leonardo.mirandas@gmail.com>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.Archetypes.SQLStorage import *
from Products.GrupoVisao.config import *

##code-section module-header #fill in your manual code here

from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.Message import Message
from Products.GrupoVisao.htmlrender import renderHTML

##/code-section module-header

schema = Schema((

    TextField(
        name='corpo',
        allowable_content_types=('text/plain', 'text/structured', 'text/html', 'application/msword',),
        widget=RichWidget(
            label='Corpo',
            label_msgid='GrupoVisao_label_corpo',
            i18n_domain='GrupoVisao',
        ),
        default_output_type='text/html'
    ),

),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

jornal_schema = BaseSchema.copy() + \
    schema.copy()

##code-section after-schema #fill in your manual code here
##/code-section after-schema

class jornal(BaseContent):
    """
    """
    security = ClassSecurityInfo()
    __implements__ = (getattr(BaseContent,'__implements__',()),)

    # This name appears in the 'add' box
    archetype_name = 'Newsletter'

    meta_type = 'jornal'
    portal_type = 'jornal'
    allowed_content_types = []
    filter_content_types = 0
    global_allow = 1
    #content_icon = 'jornal.gif'
    immediate_view = 'base_view'
    default_view = 'base_view'
    suppl_views = ()
    typeDescription = "jornal"
    typeDescMsgId = 'description_edit_jornal'

    _at_rename_after_creation = True

    schema = jornal_schema

    actions =  (

       {'action': "string:${object_url}/jornal_view",
        'category': "object",
        'id': 'view',
        'name': 'View',
        'permissions': ("View",),
        'condition': 'python:1'
       },

    )
    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods
    def sendNotificationMail(self):
        #TITULO DO EMAIL
        subject = self.title_or_id()

        portal_membership = getToolByName(self, 'portal_membership')
        usuarios = portal_membership.listMemberIds()

        mailText = self.jornal_send_mail(self, jornal=self)

        for u in usuarios:
            email = portal_membership.getMemberById(u).getProperty('email', '')
            if email:
                self.sendMail(message=mailText,toAddresses=email,subject = subject)


    security.declarePublic('sendMail')
    def sendMail(self, message='', toAddresses=None, subject = ''):
        """ Manda e-mail para o responsável"""

        plone_utils = getToolByName(self, 'plone_utils')

        fromEmail = "Instituto Visao<informativo@visaoinstitutos.com.br>"
        charset     = plone_utils.getSiteEncoding()

        email = MIMEMultipart('alternative')
        email.epilogue = ''

        textPart = MIMEText(message, 'plain', charset)
        email.attach(textPart)
        htmlPart = MIMEText(renderHTML(message), 'html', charset)
        email.attach(htmlPart)
        message = str(email)

        try:
            self.MailHost.send(message = message,
                               mto = toAddresses,
                               mfrom = fromEmail,
                               subject = subject)
            log('[GrupoVisao] MESSAGE: Mandou e-mail!')
        except:
            log('[GrupoVisao] ERROR: MailHostError')





registerType(jornal, PROJECTNAME)
# end of class jornal

##code-section module-footer #fill in your manual code here
##/code-section module-footer



